--
-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--

---@type BP_Runner_C
local M = UnLua.Class()

local Math = UE.UKismetMathLibrary

local GM_RunnerClass = UE.UClass.Load("/Script/Engine.Blueprint'/MashRunner/Blueprints/GM_Runner.GM_Runner_C'")

-- function M:Initialize(Initializer)
-- end

--function M:UserConstructionScript()
--end

function M:ReceiveBeginPlay()
    self.GameMode = UE.UGameplayStatics.GetGameMode(self):Cast(GM_RunnerClass)
    self.GameMode.AnnouncedWinne:Add(self, self.OnAnnouncedWinner)
    self.GameMode.StartRace:Add(self, self.StartRace)
end

-- function M:ReceiveEndPlay()
-- end

function M:ReceiveTick(DeltaSeconds)
    self:AddMovementInput(UE.FVector(1, 0, 0), 1, false)

    self:DecreaseSpeed(DeltaSeconds)
    self:UpdateFlipbook()
end

-- function M:ReceiveAnyDamage(Damage, DamageType, InstigatedBy, DamageCauser)
-- end

-- function M:ReceiveActorBeginOverlap(OtherActor)
-- end

-- function M:ReceiveActorEndOverlap(OtherActor)
-- end

-- 减速
function M:DecreaseSpeed(DeltaSeconds)
    local Multiplier
    if self.CanRun then
        Multiplier = self.SlowMultiplier
    else
        Multiplier = 1500
    end
    local TempSpeed = self.SpeedDecreaseRamp:GetFloatValue(Math.VSize(self.CharacterMovement.Velocity)) * Multiplier
    local a = self.CharacterMovement.MaxWalkSpeed - DeltaSeconds * TempSpeed
    local NewSpeed = Math.FMax(a, 0)
    self.CharacterMovement.MaxWalkSpeed = NewSpeed
end

-- 更新翻页
function M:UpdateFlipbook()

    local NewFlipbook
    if Math.VSize(self.CharacterMovement.Velocity) > 0 then
        NewFlipbook = self.RunnerFlipbook
        self.Sprite:SetPlayRate(Math.VSize(self.CharacterMovement.Velocity) / self.MaxSpeed)
        self:PlaySound()
    else
        NewFlipbook = self.IdleFlipbook
        self.Sprite:SetPlayRate(1)
    end

    self.Sprite:SetFlipbook(NewFlipbook)
end

-- 速度提升
function M:IncreaseSpeed()

    if self.CanRun == true then
        local SpeedIncreasePreTap = self.SpeedIncreasePreTap * self.SpeedIncreaseRamp:GetFloatValue(Math.VSize(self.CharacterMovement.Velocity))
        local NewSpeed = Math.FClamp(self.CharacterMovement.MaxWalkSpeed + SpeedIncreasePreTap, 0, self.MaxSpeed)
        self.CharacterMovement.MaxWalkSpeed = NewSpeed
    end

end

-- 播放声音
function M:PlaySound()

    if self.Sprite:GetPlaybackPositionInFrames() == 1 or self.Sprite:GetPlaybackPositionInFrames() == 5 then
        if self.InPlaySound == false then
            UE.UGameplayStatics.PlaySound2D(self, self.Footstep)
            self.InPlaySound = true
        end
    else
        self.InPlaySound = false
    end

end

---左按下
function M:PowerLeftPressed()
    if self.LastInput == UE.EInput.EI_LeftInput then
    else
        self.LastInput = UE.EInput.EI_LeftInput
        self:IncreaseSpeed()
    end

end

---左松开
function M:PowerLeftReleased()

end

---右按下
function M:PowerRigPressed()
    if self.LastInput == UE.EInput.EI_RightInput then
    else
        self.LastInput = UE.EInput.EI_RightInput
        self:IncreaseSpeed()
    end
end

---右松开
function M:PowerRigReleased()

end

-- 模拟左键按下
function M:SimulatePowerLeft()
    if self.LastInput == UE.EInput.EI_LeftInput then
    else
        self.LastInput = UE.EInput.EI_LeftInput
        self:IncreaseSpeed()
    end
end

-- 模拟右键按下
function M:SimulatePowerRight()
    if self.LastInput == UE.EInput.EI_RightInput then
    else
        self.LastInput = UE.EInput.EI_RightInput
        self:IncreaseSpeed()
    end
end

function M:OnAnnouncedWinner()
    self.CanRun = false
end

-- 结束跑步
function M:FinishRace()
    self.CanRun = false
end

-- 开始跑步
function M:StartRace()
    self.CanRun = true
end

return M
